(function(){
    var wrapBox = $("#wrap");
    var dpBox = $(".dropDown-box");
    var dpBtn = $("#dropDown-btn");
    var curUrl = window.location.href;
    var arrUrl = new Array();
    var scrollHeight = window.document.body.clientHeight || window.documentElement.clientHeight;
    var tops = 600;

    var leftBox = $(".leftBox");
    var rightBox = $(".rightBox");
    var cont = $("#cont");
    var searchBox = $("#searchBox");
    var header = $("#header");
    
    function strToNumber(str){
        return parseInt(str.replace(/[^0-9]/ig, ""));
    }

    var dpBox_padTop = strToNumber(dpBox.css("padding-top"));
    var dpBox_padBottom = strToNumber(dpBox.css("padding-bottom"));
    var dpBox_height = dpBox.height() + dpBox_padTop + dpBox_padBottom;

    var surplusLeft = scrollHeight - header.height() - dpBox_height;
    var surplusRight = scrollHeight - header.height() - dpBox_height - searchBox.height();
    (function(){
        if(scrollHeight){
            leftBox.css("height", surplusLeft +"px");
            rightBox.css("height", surplusLeft +"px");
            cont.css("height", (surplusRight-20) +"px");
            tops = ($("#header").height()+$("#leftMenu").height()+125) - dpBox_height;
            $("#wrap").css("top", "-" + tops +"px");
        }
    })();


    if(curUrl.indexOf("#") > 0){
        arrUrl = curUrl.split("#");
        if(arrUrl[1] === "down"){
            openMyBox(dpBtn, dpBox, wrapBox);
        }
    }
    
    dpBtn.live("click",function(){
        if($(this).hasClass("on")){
            closeMyBox($(this), dpBox, wrapBox);
        }else{
            openMyBox($(this), dpBox, wrapBox);
        }
    });

    function closeMyBox(){
        arguments[0].removeClass("on");
        arguments[1].css('background','none');
        arguments[2].stop().animate({top:"-"+tops+'px'},{queue:false, duration:500, easeOutQuad: 'easeOutBounce'});
    }

    function openMyBox(){
        arguments[0].addClass("on");
        arguments[1].css('background','url("image/transparent.png") repeat');
        arguments[2].stop().animate({top:'0'},{queue:false, duration:600, easeOutQuad: 'easeOutBounce'});
    }

    // $("#leftMenu ul li").hover(function(){
    //     $(this).siblings().find("a:first").removeClass("over");
    //     $(this).find("a:first").addClass("over");
    //     $(this).siblings().find(".secondMenu").removeClass("cur");
    //     $(this).find(".secondMenu").addClass("cur");
    // });
    $("#leftMenu dl dd").hover(function(){
        $(this).siblings().find("a").removeClass("over");
        $(this).find("a").addClass("over");
    });

    $("#cont").jscroll({
        W:"5px",
        Btn:{btn:false},
        Bg:"#000",
        Bar:{
            Pos:"up",
            Bg:{
                Out:"#7b7b7c",
                Hover:"#7b7b7c",
                Focus:"#5b5b5c"
            },
            Bd:{
                Out:"#000",
                Hover:"#000"
            }
        }
    });

    $("#contShop li").dbRotate2D();
    
    //动态导航        
    var navFirstLi = $("#leftMenu li");
    var tempNum = 0;
    $.each(navFirstLi, function(i){
        if(navFirstLi.eq(i).find("a").hasClass("cur")){
            tempNum = i;
        }
    });
    navFirstLi.hover(function(event){
        e = window.event || event;
        e.stopPropagation();
        var _this = $(this);
        trigger = setTimeout(function(){
            if(_this.index() == tempNum){
                _this.find("a").addClass("cur").css("background-position","-100px 0");
            }
            else{
                _this.siblings().find("a").removeClass("cur").css("background-position","0 0");
                
                _width = _this.find("a.nav").width() * (-1);
                _this.find("a.nav").stop().animate({
                    backgroundPosition:_width + "px " + "0"
                }, 200);
            }
        }, 100);
        
    }, function(event){
        clearTimeout(trigger);
        e = window.event || event;
        e.stopPropagation();
        if($(this).index() == tempNum){
            navFirstLi.eq(tempNum).find("a").addClass("cur").css("background-position","-100px 0");
        }else{
            $(this).find("a.nav").stop().animate({
                backgroundPosition:"0 0"
            }, 200);
            navFirstLi.eq(tempNum).find("a").addClass("cur").css("background-position","-100px 0");
        }
        $("#leftMenu ul li").hover(function(){
            that = $(this);
            that.siblings().find("a:first").removeClass("over");
            that.find("a:first").addClass("over");
            that.siblings().find(".secondMenu").removeClass("cur");
            that.find(".secondMenu").addClass("cur");
        });
    });

    //选择框插件调用
    if($("#cate").length!=0 || $("#sort").length!=0){
        $("#cate, #sort").select2({
            width:"180px"
        });
    }
})();

//图片替换
$("#cont .ct .m-right .mi ul li").click(function(){
    var originImg = $(this).find("img").attr("origin-img");
    var originUrl = $(this).find("img").attr("origin-url");
    var originTitle = $(this).find("img").attr("origin-title");
    var originTime = $(this).find("img").attr("origin-time");
    $("#cont .ct .m-left .big-pic img").attr("src", originImg);
    $("#cont .ct .m-left p a").attr("href", originUrl);
    $("#cont .ct .m-left p a span.mt_title").html(originTitle);
    $("#cont .ct .m-left p a span.mt_time").html(originTime);
});
